package com.example.demo.controller;

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping(value = "/admin/v1")
public class SsoController {
    @GetMapping("first")
    public Map<String,Object> firstResp(HttpServletRequest request){
        Map<String,Object> map=new HashMap<>();
        request.getSession().setAttribute("request Url",request.getRequestURL());
        map.put("request Url",request.getRequestURL());
        return map;
    }
    @GetMapping("session")
    public Object sessions(HttpServletRequest request){
        Map<String,Object> map=new HashMap<>();
        map.put("sessionId",request.getSession().getId());
        map.put("message",request.getSession().getAttribute("request Url"));
        return map;
    }

    //往8100的session添加属性
    @GetMapping("put")
    public void put(HttpSession session){
        session.setAttribute("yuelin","馥铧");
    }
    //从8101获取seesion属性
    //测试结果，session可以共享
//    {"session":"馥铧","url":"http://localhost:8100//admin/v1/get"}

//    {"session":"馥铧","url":"http://localhost:8101//admin/v1/get"}
    @GetMapping("get")
    public Map<String,Object> get(HttpServletRequest request){
        Map<String,Object> map=new HashMap<>();
        map.put("url",request.getRequestURL());
        map.put("session",request.getSession().getAttribute("yuelin"));
        return map;
    }
}
